using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base.Exceptions;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public class TestDMPropertyColEditorControl
    {

        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();

        }

        [Test]
        public void Test_Construct_NullControlFactory_ShouldRaiseAppropriateError()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new DMPropertyAndRuleEditorControl(null);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (HabaneroArgumentException ex)
            {
                StringAssert.Contains("The argument 'controlFactory' is not valid", ex.Message);
                StringAssert.Contains("Cannot create an 'DMPropertySelectorControl' if the control factory is null", ex.Message);
            }
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            DMPropertyAndRuleEditorControl control = new DMPropertyAndRuleEditorControl(GetControlFactory());
            //---------------Test Result -----------------------
            Assert.IsNotNull(control.GridControl);
            Assert.IsNotNull(control.PropertySelectorControl);
            Assert.IsNotNull(control.PropertyEditorControl);
            Assert.AreEqual(3, control.Controls.Count, "Two controls and the splitter");
            Assert.IsFalse(control.PropertySelectorControl.Enabled);
            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void Test_SetBusinessObject_SetsUpProperties()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.BusinessObject = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreSame(dmClass, control.BusinessObject);
            Assert.AreEqual(dmClass.Properties.Count, control.Properties.Count);
            Assert.AreSame(dmClass, control.PropertySelectorControl.BusinessObject);

            Assert.IsTrue(control.PropertySelectorControl.Enabled);
            Assert.IsTrue(control.Enabled);
        }
        [Test]
        public void Test_SetDMClass_SetsUpProperties()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreEqual(dmClass.Properties.Count, control.Properties.Count);
            Assert.AreEqual(dmClass.Properties.Count, control.Properties.Count);
            Assert.AreSame(dmClass, control.PropertySelectorControl.BusinessObject);

        }

        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetDMClassEnables()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
      
            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
        }

        [Test]
        public void Test_SelectDMProperty_SetsPropertyInEditorControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
            control.Class = dmClass;
            //----------------Assert Preconditions---------------
            Assert.IsTrue(dmClass.Properties.Count >1);
            //---------------Execute Test ----------------------
            control.SelectedBusinessObject = dmClass.Properties[1];
            //---------------Test Result -----------------------
            Assert.IsNotNull(control.PropertyEditorControl.BusinessObject);
        }

        [Test]
        public void TestSetToNullDisables()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory()) { Class = dmClass };
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);  
        }

        [Test]
        public void TestSetBusinessObjectDMProperty()
        {
            //---------------Set up test pack-------------------
            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
            TestUtilsShared.CreateSavedDMProperty(bo);

            DMPropertyAndRuleEditorControl control = new DMPropertyAndRuleEditorControl(GetControlFactory()) { Class = bo };
            DMProperty dmProperty = bo.Properties[0];           
            
            //---------------Execute Test ----------------------
            control.Property = dmProperty;
            
            //---------------Test Result -----------------------
            Assert.AreSame(dmProperty, control.Property);
            Assert.AreSame(dmProperty, control.SelectedBusinessObject);
//            Assert.AreSame(dmProperty, control.PropertyEditor.BusinessObject);
        }

        [Test]
        public void Test_SetClassNoProps_ShouldSetSelectedBoToNulll_SetsPropertyInEditorControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMClass classNoProps = TestUtilsShared.CreateUnsavedValidDMClass();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
            control.Class = dmClass;
            Assert.IsTrue(dmClass.Properties.Count > 1);
            DMProperty property = dmClass.Properties[1];
            control.SelectedBusinessObject = property;
            //----------------Assert Preconditions---------------
            Assert.AreSame(property, control.Property);
            Assert.AreSame(property, control.PropertyEditorControl.BusinessObject);
            Assert.AreEqual(0, classNoProps.Properties.Count);
            //---------------Execute Test ----------------------
            control.Class = classNoProps;
            //---------------Test Result -----------------------
            Assert.IsNull(control.Property);
            Assert.IsNull(control.PropertyEditorControl.BusinessObject);   
        }

        [Test]
        public void Test_SetClassNull_ShouldSetSelectedBoToNulll_SetsPropertyInEditorControl()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsShared.CreateSavedDMClassWithProps();
            DMPropertyAndRuleEditorControl control =
                new DMPropertyAndRuleEditorControl(GetControlFactory());
            control.Class = dmClass;
            Assert.IsTrue(dmClass.Properties.Count > 1);
            DMProperty property = dmClass.Properties[1];
            control.SelectedBusinessObject = property;
            //----------------Assert Preconditions---------------
            Assert.AreSame(property, control.Property);
            Assert.AreSame(property, control.PropertyEditorControl.BusinessObject);
            //---------------Execute Test ----------------------
            control.Class = null;
            //---------------Test Result -----------------------
            Assert.IsNull(control.Property);
            Assert.IsNull(control.PropertyEditorControl.BusinessObject);   
        }

        private static ControlFactoryWin GetControlFactory()
        {
            return new ControlFactoryWin();
        }

//        [Test]
//        public void TestLinkBetweenDMPropertyAndPropRuleInfoControls()
//        {
//            //---------------Set up test pack-------------------
//            DMClass bo = TestUtilsShared.CreateSavedDMClassWithProps();
//            TestUtilsShared.CreateSavedDMProperty(bo);
//
//            DMPropertyAndRuleEditorControl control = new DMPropertyAndRuleEditorControl(new ControlFactoryWin());
//            control.Class = bo;
//
//            //---------------Execute Test ----------------------
//            control.Property = bo.Properties[0];
//            //---------------Test Result -----------------------
//            Assert.AreSame(control.Property, control.DMPropRuleControl.Property);
//            //---------------Tear Down -------------------------          
//        }


    }
}